from PyQt5.QtCore import Qt, QModelIndex, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QPushButton,\
                            QComboBox,\
                            QDialog,\
                            QDialogButtonBox,\
                            QGridLayout,\
                            QLabel
from PyQt5.QtSql     import QSqlQueryModel


class OpenDialog(QDialog):
    opened = pyqtSignal(str)

    def __init__(self, parent, rbkmodel):
        super().__init__(parent)
        self.setWindowTitle('Open Rulebook...')
        self.db = rbkmodel.database()
        self.query = QSqlQueryModel()
        self.query.setQuery('SELECT DISTINCT GameName From Rulebooks ORDER BY GameName ASC', self.db)
        self.parent = parent
        grid = QGridLayout()
        self.resize(500, 200)


        gamelabel = QLabel('Select Game...')
        grid.addWidget(gamelabel, 0, 0, 1, 1)
        
        self.game = QComboBox()
        self.gamelist = []
        self.gamelist.append('')
        for i in range(self.query.rowCount()):
            self.gamelist.append(self.query.record(i).value('GameName'))
        self.game.addItems(self.gamelist)
        self.game.currentIndexChanged.connect(self.gameselected)
        grid.addWidget(self.game, 0, 1, 1, 2)

        versionlabel = QLabel('Select Version...')
        grid.addWidget(versionlabel, 1, 0, 1, 1)

        self.version = QComboBox()
        self.version.currentIndexChanged.connect(self.versionselected)
        grid.addWidget(self.version, 1, 1, 1, 2)
        
        rbklabel = QLabel('Rulebook Name...')
        grid.addWidget(rbklabel, 2, 0, 1, 1)

        self.rbkname = QComboBox()
        grid.addWidget(self.rbkname, 2, 1, 1, 4)

        self.buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.buttons.accepted.connect(self.onOk)
        self.buttons.rejected.connect(self.onCancel)
        grid.addWidget(self.buttons, 3, 0, 1, 4)

        self.setLayout(grid)


    @pyqtSlot()
    def gameselected(self):
        self.version.clear()
        gamename = self.game.currentText()

        f = 'GameName = "{0}"'.format(gamename)
        self.query.setQuery('SELECT DISTINCT GameVersion FROM Rulebooks WHERE GameName = "{0}" ORDER BY GameVersion DESC'.format(gamename), self.db)
        versions = []
        versions.append('')
        for i in range(self.query.rowCount()):
            versions.append(str(self.query.record(i).value('GameVersion')))
        self.version.addItems(versions)


    @pyqtSlot()
    def versionselected(self):
        self.rbkname.clear()
        gamename = self.game.currentText()
        version = self.version.currentText()
        if not version=='':
            self.query.setQuery('SELECT DISTINCT RulebookName FROM Rulebooks WHERE GameName = "{0}" AND GameVersion = "{1}" ORDER BY RulebookName ASC'.format(gamename, version), self.db)
            rulebooks = []
            rulebooks.append('')
            for i in range(self.query.rowCount()):
                rulebooks.append(self.query.record(i).value('RulebookName'))
            self.rbkname.addItems(rulebooks)


    @pyqtSlot()
    def onOk(self):
        rbkfilename = 'Rulebooks/{0}/{1}/{2}.rbk'.format(self.game.currentText(), self.version.currentText(), self.rbkname.currentText())
        self.opened.emit(rbkfilename)
        self.close()


    @pyqtSlot()
    def onCancel(self):
        self.close()
